package wasweisichquiz;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JOptionPane;

/**
 * @version Revision 1 - MAIN Changes 
 * + Grundkonstrukt
 * + @author Erik
 */
public class Question {
	private String questionText, answer1, answer2, answer3, answer4 = null;
	private int rightAnswer; // beetween 1 - 4

	private Question(String questionText, String answer1, String answer2, String answer3, String answer4, int rightAnswer) {
		this.questionText = questionText;
		this.answer1 = answer1;
		this.answer2 = answer2;
		this.answer3 = answer3;
		this.answer4 = answer4;
		this.rightAnswer = rightAnswer;
	}

	/**
	 * Setzt den Fragentext auf aText und 
	 * ruft resetQuestionAnswersToDefault auf
	 * 
	 * @param aText
	 * 
	 * @see resetQuestionAnswersToDefault()
	 */
	Question(String aText) {
		questionText = aText;
		resetQuestionAnswersToDefault();
	}

	/**
	 * Sets the question answers to default. 
	 * Empty String "" for answer1-4 and 0 for rightAnswer
	 */
	public void resetQuestionAnswersToDefault() {
		answer1 = "";
		answer2 = "";
		answer3 = "";
		answer4 = "";
		rightAnswer = 0;
	}

	public static Vector<Question> deserializeQuestions(String filename) throws FileNotFoundException {
		Vector<Question> questionVector = new Vector<Question>();
		BufferedReader breader = new BufferedReader(new FileReader(filename));

		String actualLine = "", actualQuestionText = null;
		String actualAnswer1, actualAnswer2, actualAnswer3, actualAnswer4;
		int actualRigthAnswer = 0;
		int actualCharPosition = 0;
		int actualStringNumber = 0;
		String actualString = "";
		boolean showWarning = true;

		int countline = 0;
		
		while (actualLine != null) { // 1
			actualStringNumber = 0;

			actualAnswer1 = actualAnswer2 = actualAnswer3 = actualAnswer4 = actualQuestionText = null;
			actualRigthAnswer = 0;

			try {
				countline++;
				actualLine = breader.readLine();
				actualCharPosition = 0;
				while (actualLine != null && actualCharPosition < actualLine.length()) { // 2
					actualString = "";
					while (actualCharPosition < actualLine.length() && actualLine.charAt(actualCharPosition) != '@') {
						actualString += actualLine.charAt(actualCharPosition);
						actualCharPosition++;
					}
					actualCharPosition++;

					switch (actualStringNumber) {
					case 0:
						actualQuestionText = actualString;
						break;
					case 1:
						actualAnswer1 = actualString;
						break;
					case 2:
						actualAnswer2 = actualString;
						break;
					case 3:
						actualAnswer3 = actualString;
						break;
					case 4:
						actualAnswer4 = actualString;
						break;
					case 5:
						try {
							actualRigthAnswer = Integer.parseInt(actualString);
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
								"Folgende Datei ist besch\u00E4digt\n'" + filename + "'\n"
								+ "\nUrsache:\nIn mindestens einer Zeile steht an sechster Stelle keine Zahl!\n"
								+ "Nach dem f\u00FCnften Trennzeichen muss an letzter sechster Stelle\neine Zahl von 1 bis 4 stehen!\n\n"+
								"Fehler in Zeile: "+countline, "ERROR", JOptionPane.ERROR_MESSAGE);
						}
						break;
					default:
						break;
					}
					actualStringNumber++;

				} // 2 while

				if ((actualLine != null) && (showWarning == true) && (actualStringNumber != 6)) {
					// Warnung: In einer Zeile stehen nicht genau 5 Trennzeichen
					JOptionPane.showMessageDialog(null,
							"Folgende Datei ist m\u00F6glicherweise besch\u00E4digt\n'"
							+ filename + "'\n"
							+ "\nUrsache:\nIn mindestens einer Zeile stimmt die Anzahl der Trennzeichen nicht!\n"
							+ "Es wurden " + (actualStringNumber < 6 ? "weniger" : "mehr")
							+ " Eingabes\u00E4tze erkannt als erlaubt sind.\n\n"+
							"Fehler in Zeile: "+countline, "Warnung", 
							JOptionPane.WARNING_MESSAGE);
					showWarning = false;
				}

				if (actualLine != null) {
					// Wenn Antworten leer also NULL sind, gebe einen Leerstring
					// wieder
					if (actualAnswer1 == null) actualAnswer1 = "";
					if (actualAnswer2 == null) actualAnswer2 = "";
					if (actualAnswer3 == null) actualAnswer3 = "";
					if (actualAnswer4 == null) actualAnswer4 = "";

					questionVector.add(new Question(actualQuestionText,
							actualAnswer1, actualAnswer2, actualAnswer3,
							actualAnswer4, actualRigthAnswer));
				}

			} catch (IOException e) {
				JOptionPane.showMessageDialog(null,
					"Bei Laden der Frage ist folgender Fehler aufgetreten:\n\n" + 
					e.getLocalizedMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
			}

		} // 1 while

		return questionVector;
	}

	// --------------- GETTER and SETTER --------------- //
	
	public String getQuestionText() {
		return questionText;
	}

	public String getAnswer1() {
		return answer1;
	}

	public String getAnswer2() {
		return answer2;
	}

	public String getAnswer3() {
		return answer3;
	}

	public String getAnswer4() {
		return answer4;
	}

	public int getRightAnswer() {
		return rightAnswer;
	}

}
